﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Collections;
using NCC_SOP.app_codes;
using System.Data;

namespace NCC_SOP.Admin
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            //Set the Role List Selections
            RadioButtonList roleList = (RadioButtonList)CreateUser.CreateUserStep.ContentTemplateContainer.FindControl("PageRoleList");
            CheckBoxList reportRoleList = (CheckBoxList)CreateUser.CreateUserStep.ContentTemplateContainer.FindControl("ReportRoleList");

            //set the Page Role list
            String[] roles = Roles.GetAllRoles();
            foreach (String role in roles)
            {
                roleList.Items.Add(new ListItem(role, role));
            }

            //set the Report Role list
            try
            {
                dbConnection db = new dbConnection();
                string[] reportRoles = db.getAllRoles();
                foreach (string role in reportRoles)
                {
                    reportRoleList.Items.Add(new ListItem(role, role));
                }
            }
            catch (Exception e1)
            {
                Session["ErrorMessage"] = e1.Message;
                Response.Redirect("~/ErrorDisplayPage.aspx");
            }
        }

        //This method is called after the user is created. 
        //Using the CreateUserWizard control, user is automatically created with the Membership class.
        protected void CreatedUser(object sender, EventArgs e)
        {
            RadioButtonList roleList = (RadioButtonList)CreateUser.CreateUserStep.ContentTemplateContainer.FindControl("PageRoleList");
            string[] rolesToAdd = retrieveFunctions.getSelectedItems(roleList);
            Roles.AddUserToRoles(CreateUser.UserName, rolesToAdd);
            try
            {
                dbConnection db = new dbConnection();
                CheckBoxList reportRoleList = (CheckBoxList)CreateUser.CreateUserStep.ContentTemplateContainer.FindControl("ReportRoleList");
                string[] reportRolesToAdd = retrieveFunctions.getSelectedItems(reportRoleList);
                db.addUserIntoReportRoles(CreateUser.UserName, reportRolesToAdd);
            }
            catch (Exception e2)
            {
                Session["ErrorMessage"] = e2.Message;
                Response.Redirect("~/ErrorDisplayPage.aspx");
            }
            
        }

        protected void ValidateRole(object source, ServerValidateEventArgs args)
        {
            args.IsValid = false;

            bool hasPageRole = false;
            bool hasReportRole = false;

            RadioButtonList roleList = (RadioButtonList)CreateUser.CreateUserStep.ContentTemplateContainer.FindControl("PageRoleList");
            foreach (ListItem li in roleList.Items)
            {
                if (li.Selected)
                {
                    hasPageRole = true;
                }
            }
            CheckBoxList reportRoleList = (CheckBoxList)CreateUser.CreateUserStep.ContentTemplateContainer.FindControl("ReportRoleList");
            foreach (ListItem li in reportRoleList.Items)
            {
                if (li.Selected)
                {
                    hasReportRole = true;
                }
            }

            CustomValidator cv = (CustomValidator)CreateUser.CreateUserStep.ContentTemplateContainer.FindControl("RoleRequired");

            if (hasPageRole && hasReportRole)
            {
                args.IsValid = true;
            }
            else if (hasPageRole)
            {
                cv.ErrorMessage = "Please select a Report Role.";
            }
            else 
            {
                cv.ErrorMessage = "Please select a Page Role.";
            }
        }
    }
}